<template>
  <el-dialog title="选择UNIT" :visible.sync="visible" width="800px" :close-on-click-modal="false"
    :close-on-press-escape="false" append-to-body>

    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="UNIT名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入UNIT名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" ref="selectCourseTable" :data="courseList"
      @selection-change="handleSelectionCourseChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="UNIT名称" align="center" prop="name" />
      <el-table-column label="序号" align="center" prop="sortNo" />
      <el-table-column label="等级" align="center" prop="level" />
      <el-table-column label="上线" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.common_state" :value="scope.row.state" />
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="selectCousreData">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>

  </el-dialog>

</template>

<script>
  import {
    listCourse
  } from "@/api/bus_web/course/course";
  import store from "@/store";
  import {
    mapState
  } from 'vuex';

  export default {
    name: "SelectCourse",
    dicts: ['common_state'],
    data() {
      return {
        // 遮罩层
        loading: true,
        visible: false,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // UNIT管理表格数据
        courseList: [],
        // 弹出层标题
        title: "",
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          name: null,
          state: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          name: [{
            required: true,
            message: "UNIT名称不能为空",
            trigger: "blur"
          }],
          state: [{
            required: true,
            message: "上线不能为空",
            trigger: "change"
          }],
        }
      };
    },
    computed: {
      ...mapState({
        course: state => state.selectCourse.course,
        courseSelectModel: state => state.selectCourse.courseSelectModel
      })
    },
    methods: {
      init(level) {
        this.visible = true
        this.reset()
        this.queryParams.level = level
        this.resetQuery()
      },
      /** 查询UNIT管理列表 */
      getList() {
        this.loading = true;
        if (this.schoolId) {
          this.queryParams.includeSchoolId = this.schoolId
        }
        listCourse(this.queryParams).then(response => {
          this.courseList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.visible = false
        this.$refs.selectCourseTable.clearSelection()
        this.$store.dispatch('selectCourse/cleanCourse')
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          name: null,
          state: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionCourseChange(selection) {
        if (selection.length > 1 && this.courseSelectModel == 2) {
          selection = []
          this.$refs.selectCourseTable.clearSelection()
          this.$store.dispatch('selectCourse/cleanCourseData')
          this.$modal.msgError("最多只能选择1条数据")
          return
        }
        selection.map(item => {
          this.$store.dispatch('selectCourse/setCourse', item)
        })
      },

      selectCousreData() {
        if (this.course.length < 1) {
          this.$modal.msgError("请至少选中1条UNIT")
          return
        }
        this.visible = false
        this.$emit('selected-data')
      }
    }
  };
</script>
